package com.zb.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.BeanUtils;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.entity.FieldValOptionVo;
import com.zb.entity.ZjNetworkEmpFieldVal;
import com.zb.entity.ZjNetworkEmpFieldValOption;
import com.zb.mapper.ZjNetworkEmpFieldValMapper;
import com.zb.result.PageUtil;
import com.zb.service.ZjNetworkEmpFieldValOptionService;
import com.zb.service.ZjNetworkEmpFieldValService;
import com.zb.vo.NetworkFieldValVo;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 网点人员字段值表 服务实现类
 * </p>
 *
 * @author WEN
 * @since 2023-10-23
 */
@Service
public class ZjNetworkEmpFieldValServiceImpl extends ServiceImpl<ZjNetworkEmpFieldValMapper, ZjNetworkEmpFieldVal> implements ZjNetworkEmpFieldValService {
    @Resource
    private ZjNetworkEmpFieldValMapper networkEmpFieldValMapper;
    @Resource
    private ZjNetworkEmpFieldValOptionService networkEmpFieldValOptionService;

    // 保存、更改服务商人员字段
    @Override
    public void save(Long custAccId, Long networkEmpId, List<NetworkFieldValVo> fieldVals) {
        if (CollectionUtils.isEmpty(fieldVals)) {
            return;
        }
        List<ZjNetworkEmpFieldVal> adds = new ArrayList<>();
        List<ZjNetworkEmpFieldVal> updates = new ArrayList<>();
        ZjNetworkEmpFieldVal fieldVal = null;
        List<ZjNetworkEmpFieldValOption> addOptions = new ArrayList<>();
        ZjNetworkEmpFieldValOption option = null;
        for (NetworkFieldValVo vo : fieldVals) {
            fieldVal = new ZjNetworkEmpFieldVal();
            BeanUtil.copyProperties(vo, fieldVal);
            if (null != fieldVal.getId()) {
                updates.add(fieldVal);
            } else {
                fieldVal.setId(IdWorker.getId());
                fieldVal.setCustAccId(custAccId);
                fieldVal.setNetworkEmpId(networkEmpId);
                fieldVal.setNetworkEmpFieldId(vo.getFieldId());
                adds.add(fieldVal);
            }
            for (FieldValOptionVo optVo : vo.getOptVos()) {
                option = new ZjNetworkEmpFieldValOption();
                option.setId(IdWorker.getId());
                option.setCustAccId(custAccId);
                option.setNetworkEmpId(networkEmpId);
                option.setNetworkEmpFieldValId(fieldVal.getId());
                option.setOptValue(null == optVo.getOptValue()? "" : optVo.getOptValue());
                addOptions.add(option);
            }
        }
        if (CollectionUtil.isNotEmpty(adds)) this.saveBatch(adds);
        if (CollectionUtil.isNotEmpty(updates)) this.updateBatchById(updates);
        networkEmpFieldValOptionService.deleteByNetworkEmpId(custAccId, networkEmpId);
        if (CollectionUtil.isNotEmpty(addOptions)) networkEmpFieldValOptionService.saveBatch(addOptions);
    }

    // 根据服务人员ID获取服务人员自定义字段信息
    @Override
    public List<ZjNetworkEmpFieldVal> getByNetworkEmpId(Long custAccId, Long networkEmpId) {
        QueryWrapper<ZjNetworkEmpFieldVal> wrapper = new QueryWrapper<>();
        wrapper.eq("cust_acc_id", custAccId).eq("network_emp_id", networkEmpId);
        return this.list(wrapper);
    }

    // 根据服务人员字段ID获取与服务人员关联的字段ID
    @Override
    public List<Long> getRelationByNetworkEmpFieldId(List<Long> networkEmpFieldIds) {
        if (CollectionUtil.isEmpty(networkEmpFieldIds)) {
            return new ArrayList<>();
        }
        QueryWrapper<ZjNetworkEmpFieldVal> wrapper = new QueryWrapper<>();
        wrapper.eq("network_emp_field_id", networkEmpFieldIds);
        return this.list(wrapper).stream().map(ZjNetworkEmpFieldVal::getNetworkEmpFieldId).distinct().toList();
    }
}
